package com.liveneo.adapter.mobile.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.liveneo.adapter.base.entity.PrpmMobilePushMsg;
import com.liveneo.adapter.base.mapper.PrpmMobilePushMsgMapper;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.mobile.model.request.CallbackMsgRequest;
import com.liveneo.adapter.mobile.model.request.MsgListRequest;
import com.liveneo.adapter.mobile.model.request.UpdateStatesRequest;
import com.liveneo.adapter.mobile.service.MobilePushService;
import com.liveneo.adapter.mobile.service.ValidateService;
import com.liveneo.system.base.utils.AppException;

/** 手机推送服务类
 * @author wangxx
 *
 */
@Service
public class MobilePushServiceImpl extends BaseServiceImpl implements MobilePushService {
    @Autowired
    private PrpmMobilePushMsgMapper mobilePushMsgMapper;
    @Autowired
    private ValidateService  validateService;

    @Override
    public List<PrpmMobilePushMsg> getMsgList(MsgListRequest request) throws AppException {
        // 校验
        validateService.validateMsgList(request);
        // 获取列表
        return mobilePushMsgMapper.getMsgList(request);
    }

    @Override
    public void callbackMsg(CallbackMsgRequest request) throws AppException {
        // 校验参数
        validateService.validateCallbackMsg(request);
        // 获取参数
        String msgUuid = request.getMessageUuid();
        String receiveTime = request.getReceiveTime();
        PrpmMobilePushMsg msg = mobilePushMsgMapper.selectByPrimaryKey(msgUuid);
        // 通过ID查到消息时，更新消息的接收时间、是否接收到标识和发送状态
        if (msg != null) {
            msg.setReceiveName(receiveTime);
            msg.setIsReceive(true);
            msg.setSendStatus(true);
            mobilePushMsgMapper.updateByPrimaryKey(msg);
        }
    }

    @Override
    public void updateStates(UpdateStatesRequest request) throws AppException {
        // 校验参数
        validateService.validateMsgUpdateStatus(request);
        String msgUuid = request.getMessageUuids();
        String operatorType = request.getOperatorType();
        List<String> msgUuids = Arrays.asList(StringUtils.split(msgUuid, ","));
        if ("1".equals(operatorType)) {
            // 置成已读状态
            mobilePushMsgMapper.updateFlagReadByIds(msgUuids);
        } else if ("2".equals(operatorType)) {
            // 置成删除状态
            mobilePushMsgMapper.updateFlagDeleteByIds(msgUuids);
        }
    }
}
